package com.coffee.statistics.service.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.DateUtil;
import com.coffee.core.util.StringUtil;
import com.coffee.order.service.sku.bean.SkuOrder;
import com.coffee.order.service.sku.dao.SkuOrderDao;
import com.coffee.sku.service.sku.impl.SkuCategoryAttributeServiceImpl;
import com.coffee.statistics.api.query.StatisticsQuery;
import com.coffee.statistics.api.result.MonthStatisticsResult;
import com.coffee.statistics.api.result.StatisticsResult;
import com.coffee.statistics.api.result.TotalStatisticsResult;
import com.coffee.statistics.api.service.StatisticsService;
import com.coffee.statistics.service.bean.Statistics;
import com.coffee.statistics.service.dao.StatisticsDao;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class StatisticsServiceImpl implements StatisticsService {
    private static final Logger logger = LoggerFactory.getLogger(StatisticsServiceImpl.class);
    @Resource
    private StatisticsDao statisticsDao;
    @Resource
    private SkuOrderDao skuOrderDao;

    @Override
    public Page<StatisticsResult> getPager(StatisticsQuery param, PagerInfo pr) {
        Page<StatisticsResult> pager = new Page<StatisticsResult>(pr.getStart(), pr.getPageSize());
        Integer count = statisticsDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<Statistics> list = statisticsDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, StatisticsResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(StatisticsQuery param) {
        Integer count = statisticsDao.count(param);
        return count;
    }

    @Override
    public StatisticsResult getDetail(Integer id) {
        Statistics item = statisticsDao.findById(id);
        if (item == null) {
            return null;
        }
        StatisticsResult result = new StatisticsResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<StatisticsResult> getList(StatisticsQuery param, PagerInfo pr) {
        List<Statistics> list = statisticsDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, StatisticsResult.class);
    }

    @Override
    public List<StatisticsResult> getList(StatisticsQuery param) {
        List<Statistics> list = statisticsDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, StatisticsResult.class);
    }

    @Override
    public List<StatisticsResult> getList() {
        StatisticsQuery param = new StatisticsQuery();
        List<Statistics> list = statisticsDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, StatisticsResult.class);
    }


    @Override
    public void delete(Integer id) {
        statisticsDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(StatisticsResult result) throws BusinessException {
        Statistics item = new Statistics();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            statisticsDao.insert(item);
        } else {
            Statistics tmp = statisticsDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            statisticsDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, StatisticsResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, StatisticsResult> map = new HashMap<Integer, StatisticsResult>();
        List<Statistics> list = statisticsDao.findByIds(StringUtil.setToList(idSet));
        List<StatisticsResult> resultList = DTOUtils.convertList(list, StatisticsResult.class);
        for (StatisticsResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, StatisticsResult> getItemMap() {
        Map<Integer, StatisticsResult> map = new HashMap<Integer, StatisticsResult>();
        StatisticsQuery param = new StatisticsQuery();
        List<Statistics> list = statisticsDao.page(param, 0, Integer.MAX_VALUE);
        List<StatisticsResult> resultList = DTOUtils.convertList(list, StatisticsResult.class);
        for (StatisticsResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public void addByDay(Date startTime, Date endTime) {
        logger.info("startTime===" + startTime + ",endTime==" + endTime);
        String dateTime = DateUtil.getDateTime(startTime, DateUtil.DEFAULT_DATE_FORMAT);
        logger.info("dateTime===" + dateTime);
        Double enjoyPayPriceBySuccess = skuOrderDao.getEnjoyPayPriceBySuccess(startTime, endTime);
        logger.info("enjoyPayPriceBySuccess===" + enjoyPayPriceBySuccess);
        Double enjoyPayPriceBySuccessByPostage = skuOrderDao.getEnjoyPayPriceBySuccessByPostage(startTime, endTime);
        logger.info("enjoyPayPriceBySuccessByPostage===" + enjoyPayPriceBySuccessByPostage);
        Integer enjoyCountBySuccess = skuOrderDao.getEnjoyCountBySuccess(startTime, endTime);
        logger.info("enjoyCountBySuccess===" + enjoyCountBySuccess);
        Double enjoyPayPriceByWait = skuOrderDao.getEnjoyPayPriceByWait(startTime, endTime);
        logger.info("enjoyPayPriceByWait===" + enjoyPayPriceByWait);
        Double enjoyPayPriceByWaitByPostage = skuOrderDao.getEnjoyPayPriceByWaitByPostage(startTime, endTime);
        logger.info("enjoyPayPriceByWaitByPostage===" + enjoyPayPriceByWaitByPostage);
        Integer enjoyCountByWait = skuOrderDao.getEnjoyCountByWait(startTime, endTime);
        logger.info("enjoyCountByWait===" + enjoyCountByWait);
        Double buyPayPriceBySuccess = skuOrderDao.getBuyPayPriceBySuccess(startTime, endTime);
        logger.info("buyPayPriceBySuccess===" + buyPayPriceBySuccess);
        Double buyPayPriceBySuccessByPostage = skuOrderDao.getBuyPayPriceBySuccessByPostage(startTime, endTime);
        logger.info("buyPayPriceBySuccessByPostage===" + buyPayPriceBySuccessByPostage);
        Integer buyCountBySuccess = skuOrderDao.getBuyCountBySuccess(startTime, endTime);
        logger.info("buyCountBySuccess===" + buyCountBySuccess);
        Integer buyCountBySuccessAndCar = skuOrderDao.getBuyCountBySuccessAndCar(startTime, endTime);
        logger.info("buyCountBySuccessAndCar===" + buyCountBySuccessAndCar);
        Double refundPriceBySuccess = skuOrderDao.getRefundPriceBySuccess(startTime, endTime);
        logger.info("refundPriceBySuccess===" + refundPriceBySuccess);
        Integer refundCountBySuccess = skuOrderDao.getRefundCountBySuccess(startTime, endTime);
        logger.info("refundCountBySuccess===" + refundCountBySuccess);
        Double offlinePayPrice = skuOrderDao.getOfflinePayPrice(startTime, endTime);
        logger.info("offlinePayPrice===" + offlinePayPrice);
        Double offlinePayPricePostage = skuOrderDao.getOfflinePayPricePostage(startTime, endTime);
        logger.info("offlinePayPricePostage===" + offlinePayPricePostage);
        Integer offlineCount = skuOrderDao.getOfflineCount(startTime, endTime);
        logger.info("offlineCount===" + offlineCount);
        Integer periodizationCountBySuccess = skuOrderDao.getPeriodizationCountBySuccess(startTime, endTime);
        Double periodizationPayPriceBySuccessByPostage = skuOrderDao.getPeriodizationPayPriceBySuccessByPostage(startTime, endTime);
        Double periodizationPayPriceBySuccess = skuOrderDao.getPeriodizationPayPriceBySuccess(startTime, endTime);


        Statistics updateStatistics = new Statistics();
        updateStatistics.setTotalMoney(enjoyPayPriceBySuccess + enjoyPayPriceBySuccessByPostage +
                enjoyPayPriceByWait + enjoyPayPriceByWaitByPostage + buyPayPriceBySuccess
                + buyPayPriceBySuccessByPostage + offlinePayPrice + offlinePayPricePostage +
                periodizationPayPriceBySuccessByPostage + periodizationPayPriceBySuccess);
        updateStatistics.setSpotsMoney(buyPayPriceBySuccess + buyPayPriceBySuccessByPostage);
        updateStatistics.setFinishEnjoyMoney(enjoyPayPriceBySuccess + enjoyPayPriceBySuccessByPostage);
        updateStatistics.setWaitEnjoyMoney(enjoyPayPriceByWait + enjoyPayPriceByWaitByPostage);
        updateStatistics.setRefundMoney(refundPriceBySuccess);
        updateStatistics.setOfflineMoney(offlinePayPrice + offlinePayPricePostage);
        updateStatistics.setPeriodizationMoney(periodizationPayPriceBySuccessByPostage + periodizationPayPriceBySuccess);
        updateStatistics.setTotalNumber(periodizationCountBySuccess + enjoyCountBySuccess + enjoyCountByWait + buyCountBySuccess + buyCountBySuccessAndCar + offlineCount);
        updateStatistics.setSpotsNumber(buyCountBySuccess + buyCountBySuccessAndCar);
        updateStatistics.setFinishEnjoyNumber(enjoyCountBySuccess);
        updateStatistics.setWaitEnjoyNumber(enjoyCountByWait);
        updateStatistics.setRefundNumber(refundCountBySuccess);
        updateStatistics.setOfflineNumber(offlineCount);
        updateStatistics.setPeriodizationNumber(periodizationCountBySuccess);
        updateStatistics.setCreateDate(dateTime);
        updateStatistics.setYearDate(DateUtil.getDateTime(startTime, DateUtil.DEFAULT_YEAR_FORMAT));
        updateStatistics.setMonthDate(DateUtil.getDateTime(startTime, DateUtil.DEFAULT_MONTH_FORMAT));
        Statistics statistics = statisticsDao.findByDate(dateTime);
        if (statistics != null) {
            updateStatistics.setId(statistics.getId());
            statisticsDao.updateById(updateStatistics);
        } else {
            statisticsDao.insert(updateStatistics);
        }

    }

    @Override
    public TotalStatisticsResult getTotalStatisticsResult(String date) {
        String year = DateUtil.getYear() + "";
        if (StringUtils.isNotBlank(date)) {
            year = date.split("-")[0];
        }
        TotalStatisticsResult totalStatisticsResult = statisticsDao.getTotalStatisticsResult(year);
        List<MonthStatisticsResult> monthStatisticsResult = statisticsDao.getMonthStatisticsResult(year);
        totalStatisticsResult.setMonthStatisticsResults(monthStatisticsResult);
        return totalStatisticsResult;
    }


}
